Shader "Unlit/EdgeDetection"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        //描边颜色
        _EdgeColor("EdgeColor", Color) = (0,0,0,0)
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }

        Pass
        {
            ZTest Always
            Cull Off
            ZWrite Off

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2f
            {
                //存储下来的对应像素点周围9个像素
                half2 uv[9] : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            //像素纹理偏移值
            half4 _MainTex_TexelSize;
            fixed4 _EdgeColor;

            v2f vert(appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                //当前像素点
                half2 uv = v.texcoord;
                //周围的9个像素点
                o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1, -1);
                o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0, -1);
                o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1, -1);
                o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1, 0);
                o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0, 0);
                o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1, 0);
                o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1, 1);
                o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0, 1);
                o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1, 1);

                return o;
            }

            //计算对应像素点的灰度值
            fixed calcLuminance(fixed4 color)
            {
                return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
            }

            //计算Sobel对应像素点梯度值
            half Sobel(v2f o)
            {
                //横向梯度值
                half Gx[9] = {
                    -1, -2, -1,
                    0, 0, 0,
                    1, 2, 1
                };
                //纵向梯度值
                half Gy[9] = {
                    -1, 0, 1,
                    -2, 0, 2,
                    -1, 0, 1
                };
                half L; //灰度值
                half edgeX = 0; //横向梯度值总和
                half edgeY = 0; //纵向梯度值总和
                for (int i = 0; i < 9; i++)
                {
                    //计算对应像素点灰度值
                    L = calcLuminance(tex2D(_MainTex, o.uv[i]));
                    edgeX += L * Gx[i];
                    edgeY += L * Gy[i];
                }
                //计算对应像素点梯度值
                return abs(edgeX) + abs(edgeY);
            }

            fixed4 frag(v2f i) : SV_Target
            {
                half edge = Sobel(i);
                //采样出来的颜色和描边颜色进行插值
                fixed4 color = lerp(tex2D(_MainTex, i.uv[4]), _EdgeColor, edge);
                return color;
            }
            ENDCG
        }
    }

    Fallback Off
}